
#include<stdio.h>
#include <string.h>
#include "main.h"
#include "BC26.h"
#include "Network_communication.h"
#include "TelecomPlatform.h"

#include "user_gpio.h"
#include "user_uart_interrupt.h"

#include "user_delay.h"
#include "bstim32_timing.h"


extern uint8_t Sent_Data[100];
extern uint8_t Sample_Data[16];
extern uint16_t Battary_Val;
extern uint16_t adc_calt_value;
extern uint8_t SentTimes;
extern uint8_t Bc26_CESQ;
extern uint8_t UART0_Rx_Buf[MAX_REC_LENGTH];
extern uint16_t  UART0_Rx_cnt ;
extern uint8_t UART0_temp[REC_LENGTH];
extern uint8_t Rece_len;
extern uint8_t Recebuffer[ReceiveUdpDataMaxLen];
extern measure_value MeasureData;



uint8_t	SendMessage_To_Server(void)
{
	uint8_t errcode;
	uint8_t i = 0;
	uint8_t j = 0;
	uint8_t sendbuffer[IMEI_Len+ICCID_Len+12*2+1];
	uint8_t databuffer[50]={0};
	Sample_Data[1] = MeasureData.PM1_0;
	Sample_Data[0] = MeasureData.PM1_0>>8;
	Sample_Data[3] = MeasureData.PM2_5;
	Sample_Data[2] = MeasureData.PM2_5>>8;
	Sample_Data[5] = MeasureData.PM10;
	Sample_Data[4] = MeasureData.PM10>>8;
	Sample_Data[7] = MeasureData.VOC;
	Sample_Data[6] = MeasureData.VOC>>8;
	Sample_Data[9] = MeasureData.temperature;
	Sample_Data[8] = MeasureData.temperature>>8;
	Sample_Data[11] = MeasureData.humi;
	Sample_Data[10] = MeasureData.humi>>8;
	Sample_Data[12] = Bc26_CESQ;
	Sample_Data[13] = SentTimes;
	Sample_Data[14] = 0x00; 
	Sample_Data[15] = 0xAA; 
	for(i=0;i<16;i++)
	{
		sprintf(Sent_Data+44+j*2,"%02x",Sample_Data[i]);
		++j;
	}
	Sent_Data[76] = 0;
	i = 0;
	while (++i < 2) 
	{
		
		errcode = SendDataToTelecomPlatform(Sent_Data,76);
		if (errcode == TRUE)
		{
			UART0_Rx_cnt = 0;
			return	TRUE;
		}
		else
		{
			HAL_Delay(4000);
		}
	}
	UART0_Rx_cnt = 0;
	return	FALSE;
}
void Receive_Message_From_Server(void)
{
	uint8_t i,j;
	uint8_t	buffer[ReceiveUdpDataMaxLen]={0},len;
	for(j=0;j<ReceiveUdpDataMaxLen;j++)
		Recebuffer[j] = 0;
	Rece_len = 0;
	len=ReceiveMessageFromTelecomPlatform(buffer);
	for(j=0;j<len;j++)
		Recebuffer[j] = buffer[j];
	Rece_len = len;
	UART0_Rx_cnt = 0;












}
uint8_t	SendAckToServer(void)
{
	uint8_t i = 0,j=0, errcode;
	
	uint8_t sendbuffer[IMEI_Len+ICCID_Len+10*2+1] = {
						  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						  0x09,
						  0xC1, 0XC1,
						  0X05, 
						  0x00,0x00,
						  0x00,0x00,
						  0x00,0x00,
						  0x00};
	uint8_t databuffer[50]={0};





	
	
	
	
	
	j=0;
	for(i=IMEI_Len+ICCID_Len;i<IMEI_Len+ICCID_Len+10;i++)
	{
		sprintf(databuffer+j*2,"%02x",sendbuffer[i]);
		++j;
	}
	memcpy(sendbuffer,BC26_IMEI,IMEI_Len);
	memcpy(sendbuffer+IMEI_Len,BC26_ICCID,ICCID_Len);
	memcpy(sendbuffer+IMEI_Len+ICCID_Len,databuffer,10*2);
	sendbuffer[IMEI_Len+ICCID_Len+10*2]=0;
	
	i = 0;
	while (++i < 3) 
	{
		
		errcode = SendDataToTelecomPlatform(sendbuffer, sizeof(sendbuffer)-1);
		if (errcode == TRUE)
		{
			return	TRUE;
		}
		else
		{
			HAL_Delay(3000);
		}
	}
	return	FALSE;
}
void	CMDProcess(uint8_t *buf,uint8_t len)
{
	
}